import scrapy
from miappspider.items import AppDetailsItem
import pymysql

class MipaSpider(scrapy.Spider):
    conntect = pymysql.Connect(
            host="localhost",
            port=3306,
            user="root",
            passwd="123456",
            db="misoft",
            charset="utf8"
    )
    cur = conntect.cursor()
    cur.execute('SELECT package_name FROM softprofile ORDER BY app_id')
    result = cur.fetchall()
    # print(type(result))

    name = 'mipa'
    # allowed_domains = ['app.mi.com']
    start_urls = ['https://app.mi.com/details?id='+str(i[0]) for i in result]

    # start_urls = ['https://app.mi.com/details?id=com.jkwl.image.conversion']

    def parse(self, response):
        app = AppDetailsItem()
        app['logo'] = response.xpath(".//div[@class='app-info']/img/@src").extract()[0]
        app['name'] = response.xpath(".//div[@class='app-info']/div[@class='intro-titles']/h3/text()").extract()[0]
        app['size'] = response.xpath(".//div[@style='width:100%; display: inline-block']//div[@style='float:right;']/text()").extract()[0]
        app['appid'] = response.xpath(".//div[@style='width:100%; display: inline-block']//div[@style='float:right;']/text()").extract()[1]
        app['version'] = response.xpath(".//div[@style='width:100%; display: inline-block']//div[@style='float:right;']/text()").extract()[2]
        app['author'] = response.xpath(".//div[@style='width:100%; display: inline-block']//div[@style='float:right;']/text()").extract()[3]
        app['updatetime'] = response.xpath(".//div[@style='width:100%; display: inline-block']//div[@style='float:right;']/text()").extract()[4]
        app['packagename'] = response.xpath(".//div[@style='width:100%; display: inline-block']//div[@style='float:right;']/text()").extract()[7]
        app['images'] = response.xpath(".//div[@id='J_thumbnail_wrap']/img/@src")[0].extract()
        app['info'] = "".join(response.xpath(".//div[@class='app-text']/p/text()").extract())
        app['download'] = response.urljoin(response.xpath(".//div[@class='app-info-down']/a/@href").extract()[0].split('?')[0])
        yield app
